﻿Imports Odyssee.WebControles
Imports OpenDelib.ServiceContacts

Public Class CtrlSelectionContacts
    Inherits SelectionControle(Of Contact, ContactsClient, ParametresSuppressionContact, CriteresRechercheContact)

    Protected Overrides ReadOnly Property LibelleElement As String
        Get
            Return "contact"
        End Get
    End Property
    Protected Overrides ReadOnly Property LibelleFeminin As Boolean
        Get
            Return False
        End Get
    End Property
    Protected Overrides ReadOnly Property LibelleApostrophe As Boolean
        Get
            Return False
        End Get
    End Property
    Protected Overrides ReadOnly Property UrlFicheElement As String
        Get
            Return "~/Tiers/FicheContact.aspx"
        End Get
    End Property
    Protected Overrides ReadOnly Property EntiteParametrageType As Integer
        Get
            Return 13
        End Get
    End Property
    Protected Overrides ReadOnly Property GridViewResultats As AcroGridView
        Get
            Return gvResultats
        End Get
    End Property
    Protected Overrides ReadOnly Property ComboType As DropDownList
        Get
            Return cmbType
        End Get
    End Property
    Protected Overrides ReadOnly Property BoutonRechercher As Button
        Get
            Return btnRechercher
        End Get
    End Property
    Protected Overrides ReadOnly Property DivRecherche As HtmlGenericControl
        Get
            Return divCriteres
        End Get
    End Property
    Protected Overrides ReadOnly Property DivGridView As HtmlGenericControl
        Get
            Return divResultats
        End Get
    End Property
    Protected Overrides ReadOnly Property FirstControlFocused As Control
        Get
            Return Me.txtNom
        End Get
    End Property


    Protected Overrides Function InitialiserParametresSuppression(cle As Object) As ParametresSuppressionContact
        If Not IsNumeric(cle) Then Return Nothing
        If CInt(cle) <= 0 Then Return Nothing

        Dim params As New ParametresSuppressionContact
        params.Authentication = New AuthenticationParameters
        params.Authentication.Token = Tools.GetInstance.getToken
        params.CleContact = CInt(cle)
        params.SuppressionTotale = True

        Return params
    End Function
    Protected Overrides Sub Supprimer(svc As ContactsClient, params As ParametresSuppressionContact, ByRef ok As Boolean, ByRef messageErreur As String)
        Dim results As OperationResult
        results = svc.Suppression(params)
        ok = results.OK
        messageErreur = results.ErrorMessage
    End Sub

    Protected Overrides Function InitialiserCriteresRecherche() As CriteresRechercheContact
        Dim params As New CriteresRechercheContact
        params.Authentication = New AuthenticationParameters
        params.Authentication.Token = Tools.GetInstance.getToken
        params.Nom = Me.txtNom.Text
        params.Prenom = Me.txtPrenom.Text
        params.Type = Me.cmbType.SelectedValue
        Return params
    End Function
    Protected Overrides Function Rechercher(svc As ContactsClient, criteres As CriteresRechercheContact, ByRef ok As Boolean, ByRef messageErreur As String) As List(Of Contact)
        Dim results As ResultatRechercheContact = svc.Recherche(criteres)
        If results Is Nothing Then Return Nothing
        ok = results.OK
        messageErreur = results.ErrorMessage
        Return results.Contacts
    End Function


    'Inherits System.Web.UI.UserControl

    'Public ReadOnly Property ResultatsSelectionnes As List(Of Contact)
    '    Get
    '        Dim contacts As New List(Of Contact)

    '        For Each row As GridViewRow In gvResultats.Rows
    '            If CType(row.Cells(0).Controls(0), CheckBox).Checked Then contacts.Add(Tools.GetRowDataItem(Of Contact, List(Of Contact))(Me.gvResultats, row))
    '        Next

    '        Return contacts
    '    End Get
    'End Property

    'Public Sub Initialiser(afficherCasesSelection As Boolean, afficherBoutons As Boolean)
    '    Me.gvResultats.Columns(0).Visible = afficherCasesSelection
    '    Me.gvResultats.Columns(Me.gvResultats.Columns.Count - 1).Visible = afficherBoutons

    '    If Not IsPostBack Then
    '        Me.divResultats.Visible = False

    '        ' Chargement des types
    '        Me.cmbType.Items.Clear()
    '        Using svc As New ServiceParametre.ParametresClient
    '            Dim params As New ServiceParametre.ListeParametreParameters
    '            params.Authentication = New ServiceParametre.AuthenticationParameters
    '            params.Authentication.Token = Tools.GetInstance.getToken
    '            params.EntiteParametrage = 13

    '            Try
    '                Dim results As ServiceParametre.OIItemListResult = svc.ListeParametre(params)
    '                If results Is Nothing OrElse Not results.OK Then
    '                    Tools.NotifyErreur(Me.Page, "Impossible de charger la liste des types de contact" & If(results Is Nothing, ".", " :" & ControlChars.NewLine & results.ErrorMessage))
    '                Else
    '                    Me.cmbType.Items.Add(New ListItem("Tous", ""))
    '                    For Each item As ServiceParametre.OIItem In results.Liste
    '                        Me.cmbType.Items.Add(New ListItem(item.Libelle, item.Code))
    '                    Next
    '                    Me.cmbType.SelectedIndex = 0
    '                End If
    '            Catch ex As Exception
    '                Tools.NotifyErreur(Me.Page, ex.Message)
    '            End Try
    '        End Using

    '        Me.Page.SetFocus(Me.txtNom)
    '    End If
    'End Sub

    'Private Sub btnRechercher_Click(sender As Object, e As EventArgs) Handles btnRechercher.Click
    '    ' Lancement de la recherche de contacts
    '    RechercherContacts()
    'End Sub

    'Private Sub gvResultats_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gvResultats.RowCommand
    '    Dim cleContact As Integer = 0

    '    If Not IsNumeric(e.CommandArgument) Then Return

    '    cleContact = CInt(e.CommandArgument)
    '    If cleContact <= 0 Then Return

    '    Select Case e.CommandName.ToUpper
    '        Case "MODIFIER"
    '            Response.Redirect("~/Tiers/FicheContact.aspx?modeFiche=1&cleContact=" & cleContact)

    '        Case "SUPPRIMER"
    '            Using svc As New ContactsClient
    '                Dim params As New ParametresSuppressionContact
    '                params.Authentication = New AuthenticationParameters
    '                params.Authentication.Token = Tools.GetInstance.getToken
    '                params.CleContact = cleContact
    '                params.SuppressionTotale = True

    '                Try
    '                    Dim results As OperationResult
    '                    results = svc.Suppression(params)
    '                    If results.OK Then
    '                        Tools.NotifySuccess(Me.Page, "Suppression effectuée avec succès.")
    '                        RechercherContacts()
    '                    Else
    '                        Tools.NotifyErreur(Me.Page, results.ErrorMessage)
    '                    End If
    '                Catch ex As Exception
    '                    Tools.NotifyErreur(Me.Page, ex.Message)
    '                End Try
    '            End Using
    '    End Select
    'End Sub

    'Public Sub RechercherContacts()
    '    Me.divResultats.Visible = False

    '    Using svc As New ContactsClient
    '        Dim params As New CriteresRechercheContact
    '        params.Authentication = New AuthenticationParameters
    '        params.Authentication.Token = Tools.GetInstance.getToken
    '        params.Nom = Me.txtNom.Text
    '        params.Prenom = Me.txtPrenom.Text
    '        params.Type = Me.cmbType.SelectedValue

    '        Try
    '            Dim results As ResultatRechercheContact = svc.Recherche(params)
    '            If results Is Nothing OrElse Not results.OK Then
    '                Tools.NotifyErreur(Me.Page, "Impossible de d'effectuer la recherche de contacts" & If(results Is Nothing, ".", " :" & ControlChars.NewLine & results.ErrorMessage))
    '            Else
    '                Me.divResultats.Visible = True
    '                Me.gvResultats.DataSource = results.Contacts
    '                Me.gvResultats.DataBind()
    '            End If
    '        Catch ex As Exception
    '            Tools.NotifyErreur(Me.Page, ex.Message)
    '        End Try
    '    End Using
    'End Sub
End Class